<?php

/**
 * QuestionarioController
 * 
 * @author Diego Pessoa
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class QuestionariosController extends Zend_Controller_Action {
	
	public $perguntas;
	public $textos;
	
	private $usuario;
	
	protected $_acl;
	protected $_session;
	
	public function init() {
		$config = Zend_Registry::get ( 'config' );
		$this->_acl = $this->getFrontController ()->getParam ( 'acl' );
		$this->_session = new Zend_Session_Namespace ( $config->session->namespace );
		$this->usuario = $this->_session->storage;
		$this->textos = new Textos ( );
		$this->view->aba = 'admintextos';
		if (isset ( $this->usuario )) {
			$this->view->perfil = $this->usuario->perfil;
		}
		
		$this->perguntas = new Perguntas ( );
	}
	
	public function preDispatch() {
		if (! Zend_Auth::getInstance ()->hasIdentity ()) {
			$this->view->logado = 0;
			$this->_redirect ( '/login' );
		} else {
			$config = Zend_Registry::get ( 'config' );
			$this->_session = new Zend_Session_Namespace ( $config->session->namespace );
			$this->_usuario = $this->_session->storage;
			$this->view->logado = 1;
		}
	}
	
	public function administrarAction() {
		$this->_forward ( 'index' );
	}
	
	public function indexAction() {
		$id_texto = ( int ) $this->getRequest ()->getParam ( 'id' );
		$this->view->id_texto = $id_texto;
		$this->view->perguntas = $this->perguntas->findByTexto ( $id_texto );
		$this->view->texto = $this->textos->find ( $id_texto )->current ();
		$texto = $this->textos->find ( $id_texto )->current ();
		
		//$this->view->texto_aux = utf8_encode($texto->texto);
		$this->view->texto_aux = ($texto->texto);
	}
	
	public function novoAction() {
		$this->view->id_texto = ( int ) $this->getRequest ()->getParam ( 'id' );
		$this->view->acao = 'cadastrar';
		$this->render ( 'formulario' );
	}
	
	public function editarAction() {
		$id = ( int ) $this->getRequest ()->getParam ( 'id' );
		$pergunta = $this->perguntas->find ( $id )->current ();
		$this->view->id_texto = $pergunta->id_texto;
		$this->view->pergunta = $pergunta;
		$this->view->acao = 'editar';
		$this->render ( 'formulario' );
	}
	
	public function salvarAction() {
		
		if (! $this->getRequest ()->isPost ()) {
			return $this->_forward ( 'index' );
		}
		
		$params = new Zend_Filter_Input ( null, null, $this->getRequest ()->getParams () );
		
		$data = array ('pergunta' => $params->pergunta, 'id_texto' => $params->id_texto );
		
		if ($params->id) {
			$this->perguntas->update ( $data, "id = '" . $params->id . "'" );
		} else {
			$this->perguntas->insert ( $data );
		}
		
		$this->_redirect ( 'questionarios/administrar/id/' . $params->id_texto );
	}
	
	public function apagarAction() {
		$params = new Zend_Filter_Input ( null, null, $this->getRequest ()->getParams () );
		$pergunta = $this->perguntas->find ( $params->id )->current ();
		if ($pergunta)
			$this->perguntas->delete ( $params->id );
		$this->_redirect ( "questionarios/administrar/id/$pergunta->id_texto" );
	}
	
	public function responderAction() {
		$params = new Zend_Filter_Input ( null, null, $this->getRequest ()->getParams () );
		$idTexto = ( int ) $params->idTexto;
		$respostas = new Respostas ( );
		foreach ( $params->respostas as $pergunta => $resposta ) {
			$resposta = array ('resposta' => $resposta, 'pergunta' => $pergunta, 'usuario' => $this->usuario->id );
			$respostas->insert ( $resposta );
		}
		$this->_redirect ( "textos/ver/id/" . $idTexto );
	}
	
	public function respostasAction() {
		$params = new Zend_Filter_Input ( null, null, $this->getRequest ()->getParams () );
		$pergunta = $this->perguntas->find ( $params->pergunta )->current ();
		$this->view->pergunta = $pergunta;
		$this->view->texto = $this->textos->find ( $pergunta->id_texto )->current ();
		
	//$this->view->respostas = $this->perguntas->findRespostasByPerguntas();
	}
	
	public function corrigirAction() {
		$params = new Zend_Filter_Input ( null, null, $this->getRequest ()->getParams () );
		$respostas = new Respostas ( );
		if (! $this->getRequest ()->isPost ()) {
			$this->view->resposta = $respostas->find ( $params->resposta )->current ();
			$pergunta = $this->perguntas->find ( $this->view->resposta->pergunta )->current ();
			$this->view->pergunta = $pergunta;
			$this->view->texto = $this->textos->find ( $pergunta->id_texto )->current ();
		} else {
			$correcoes = new Correcoes ( );
			$pergunta = $respostas->find ( $params->resposta )->current ()->findParentPerguntas ();
			
			$correcao = array ('resposta' => $params->resposta, 'nota' => $params->nota, 'texto' => $params->texto );
			if (! $params->id)
				$correcoes->insert ( $correcao ); else
				$correcoes->update ( $correcao );
			
			$this->_redirect ( 'questionarios/respostas/pergunta/' . $pergunta->id );
		}
	}

	public function correcaoAction() {
		$params = new Zend_Filter_Input ( null, null, $this->getRequest ()->getParams () );
		$idResposta = $params->resposta;
		$respostas = new Respostas();
		$resposta = $respostas->find($idResposta)->current();
		$this->view->resposta = $resposta;	
	}
}
